{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/ubuntu/Document/pyamlsql\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "from pathlib import Path\n",
    "p = Path(\".\").absolute().parent\n",
    "print(p)\n",
    "sys.path.append(str(p))\n",
    "from pyamlsql import YamlSql, IF\n",
    "from pyamlsql.sql_format_parameter import format_parameter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sql = YamlSql()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"true 'value'\""
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"sql0\" in sql.sqls:\n",
    "    sql.sqls.pop(\"sql0\")\n",
    "sql.add_sql(\n",
    "    \"sql0\", \"true {value1}\"\n",
    ")\n",
    "sql.get_format(\"sql0\", {\"value1\": \"value\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9.27 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit -r 1 -n 1000\n",
    "sql.get_format(\"sql0\", {\"value1\":\"value\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "423 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit -r 1 -n 1000\n",
    "statement = sql.sqls[\"sql0\"]\n",
    "format_parameter(statement.get_template_without_cache({}), {\"value1\":\"value\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"true 'value'\""
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"sql1\" in sql.sqls:\n",
    "    sql.sqls.pop(\"sql1\")\n",
    "sql.add_sql(\n",
    "    \"sql1\", \"{{ statement1 }} {value1}\",\n",
    "    {\n",
    "        \"statement1\": IF(\"condition1\", \"true\", \"false\")\n",
    "    }\n",
    ")\n",
    "sql.get_format(\"sql1\", {\"value1\": \"value\"}, {\"condition1\":True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9.34 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit -r 1 -n 1000\n",
    "sql.get_format(\"sql1\", {\"value1\":\"value\"}, {\"condition1\":True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "538 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit -r 1 -n 1000\n",
    "statement = sql.sqls[\"sql1\"]\n",
    "format_parameter(statement.get_template_without_cache({\"condition1\":True}), {\"value1\":\"value\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"true true 'value'\""
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"sql2\" in sql.sqls:\n",
    "    sql.sqls.pop(\"sql2\")\n",
    "sql.add_sql(\n",
    "    \"sql2\", \"{{ statement1 }} {{ statement2 }} {value1}\",\n",
    "    {\n",
    "        \"statement1\": IF(\"condition1\", \"true\", \"false\"),\n",
    "        \"statement2\": IF(\"condition2\", \"true\", \"false\")\n",
    "    }\n",
    ")\n",
    "sql.get_format(\"sql2\", {\"value1\": \"value\"}, {\"condition1\":True, \"condition2\":True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9.73 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit -r 1 -n 1000\n",
    "sql.get_format(\"sql2\", {\"value1\": \"value\"}, {\"condition1\":True, \"condition2\":True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "645 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit -r 1 -n 1000\n",
    "statement = sql.sqls[\"sql2\"]\n",
    "format_parameter(statement.get_template_without_cache({\"condition1\":True,\"condition2\":True}), {\"value1\":\"value\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"true true true 'value'\""
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if \"sql3\" in sql.sqls:\n",
    "    sql.sqls.pop(\"sql3\")\n",
    "sql.add_sql(\n",
    "    \"sql3\", \"{{ statement1 }} {{ statement2 }} {{ statement3 }} {value1}\",\n",
    "    {\n",
    "        \"statement1\": IF(\"condition1\", \"true\", \"false\"),\n",
    "        \"statement2\": IF(\"condition2\", \"true\", \"false\"),\n",
    "        \"statement3\": IF(\"condition3\", \"true\", \"false\")\n",
    "    }\n",
    ")\n",
    "sql.get_format(\"sql3\", {\"value1\": \"value\"}, {\"condition1\":True, \"condition2\":True, \"condition3\":True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10.5 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit -r 1 -n 1000\n",
    "sql.get_format(\"sql3\", {\"value1\": \"value\"}, {\"condition1\":True, \"condition2\":True,\"condition3\":True})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "823 µs ± 0 ns per loop (mean ± std. dev. of 1 run, 1,000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit -r 1 -n 1000\n",
    "statement = sql.sqls[\"sql3\"]\n",
    "format_parameter(statement.get_template_without_cache({\"condition1\":True,\"condition2\":True,\"condition3\":True}), {\"value1\":\"value\"})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "commodity-dev",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
